var express = require("express");
var router = express.Router();
var db = require("../model/index");
/* 查询订单信息 */
router.get("/orders/getallor", function (req, res, next) {
    let mysql = `SELECT ordernum,type,paymethod,userid,subtime,source FROM orders 
    WHERE type='待付款'`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
router.get("/orders/getallor1", function (req, res, next) {
    let mysql = `SELECT ordernum,type,paymethod,userid,subtime,source FROM orders 
    WHERE type='待发货'`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
router.get("/orders/getallor2", function (req, res, next) {
    let mysql = `SELECT ordernum,type,paymethod,userid,subtime,source FROM orders 
    WHERE type='已发货'`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
router.get("/orders/getallor3", function (req, res, next) {
    let mysql = `SELECT ordernum,type,paymethod,userid,subtime,source FROM orders 
    WHERE type='已完成'`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
router.get("/orders/getallor4", function (req, res, next) {
    let mysql = `SELECT ordernum,type,paymethod,userid,subtime,source FROM orders 
    WHERE type='已关闭'`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
/* 查询所有商品 */
router.get("/orders/getallorders", function (req, res, next) {
    let mysql = ` SELECT t1.id,t1.ordernum,t1.subtime,t1.userid,t1.paymethod,t1.handletime,t1.source,t2.phone,t1.money,t1.type
    FROM orders AS t1 LEFT JOIN USER AS t2 ON t1.userid=t2.id`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
router.get("/orders/getChaXun1", function (req, res, next) {
    let mysql = `SELECT * FROM orders 
    WHERE ordernum LIKE '%${req.query.ordernum}%'   AND userid LIKE '%${req.query.userid}%'
    AND SUBTIME LIKE '%${req.query.time}%'`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询订单列表失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询订单列表成功",
            data: data,
        });
    });
});
router.post("/refund_information1", function (req, res, next) {
    let mysql = `SELECT t1.id,t1.ordernum,t1.subtime,t1.userid,t1.paymethod,t1.handletime,t1.source,t2.phone,t1.money,t1.type
    FROM orders AS t1 LEFT JOIN USER AS t2 ON t1.userid=t2.id `;
    for (let i = 0; i < req.body.length; i++) {
        mysql = mysql + `id=${req.body[i].id}` + " or ";
    }
    mysql = mysql.replace(/or $/g, "");
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "批量删除退款信息失败，请稍后重试",
                data: err,
            });
        }
        res.json({
            code: 200,
            msg: "批量删除退款信息成功",
            data: data,
        });
    });
});
module.exports = router;
